package com.lee.controller.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lee.aspect.Log;
import com.lee.base.BaseController;
import com.lee.base.Result;
import com.lee.constant.BusinessType;
import com.lee.page.TableSupport;
import com.lee.system.api.IRoleService;
import com.lee.system.entity.Role;
import com.lee.utils.ShiroUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 角色信息
 * 
 * @author LiPengFei
 *
 */
@Api("角色管理")
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

	private String prefix = "system/role";

	@Reference
	private IRoleService roleService;

	@RequiresPermissions("system:role:view")
	@GetMapping()
	public String role() {
		return prefix + "/role";
	}

	@ApiOperation("角色列表查询")
	@RequiresPermissions("system:role:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(Role role) {
		return Result.ok().page(roleService.selectRoleList(role,TableSupport.getPageDomain()));
	}

	/**
	 * 新增角色
	 */
	@RequiresPermissions("system:role:add")
	@Log(title = "角色管理", action = BusinessType.INSERT)
	@GetMapping("/add")
	public String add(Model model) {
		return prefix + "/add";
	}

	/**
	 * 修改角色
	 */
	@RequiresPermissions("system:role:edit")
	@Log(title = "角色管理", action = BusinessType.UPDATE)
	@GetMapping("/edit/{roleId}")
	public String edit(@PathVariable("roleId") Long roleId, Model model) {
		Role role = roleService.selectRoleById(roleId);
		model.addAttribute("role", role);
		return prefix + "/edit";
	}

	/**
	 * 保存角色
	 */
	@ApiOperation("保存角色")
	@RequiresPermissions("system:role:save")
	@Log(title = "角色管理", action = BusinessType.SAVE)
	@PostMapping("/save")
	@ResponseBody
	public Result save(Role role) {
		role.setCreateBy(ShiroUtils.getLoginName());
		if (roleService.saveRole(role) > 0) {
			return Result.ok();
		}
		return Result.error();
	}

	@ApiOperation("删除角色")
	@RequiresPermissions("system:role:remove")
	@Log(title = "角色管理", action = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		try {
			roleService.deleteRoleByIds(ids);
			return Result.ok("刪除成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 校验角色名称
	 */
	@ApiOperation("校验角色名唯一")
	@PostMapping("/checkRoleNameUnique")
	@ResponseBody
	public Result checkRoleNameUnique(Role role) {
		if (roleService.checkRoleNameUnique(role)) {
			return Result.ok();
		}
		return Result.error();
	}

	/**
	 * 选择菜单树
	 */
	@GetMapping("/selectMenuTree")
	public String selectMenuTree() {
		return prefix + "/tree";
	}

}